﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

using Nb.FactoryAssistant.Service.Abstracts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using TestUnitTest;
using TestUnitTest.Base;
using TianShu.LicenseAuthorizer.Abstracts.Service;
using Xunit;

namespace Test.Service
{
    public class TestWmsWarehouseTypeService : TianShuTestBase<TestUnitTestModule>
    {
        private readonly IWmsWarehouseTypeService _warehouseTypeService;
        private readonly ILiceseLoaderService _liceseLoaderService;
        public TestWmsWarehouseTypeService()
        {
            _warehouseTypeService = GetRequiredService<IWmsWarehouseTypeService>();
            _liceseLoaderService = GetRequiredService<ILiceseLoaderService>();
        }

        [Fact]
        public async Task CrudTest()
        {
            var testFilePath = $"C:/Users/liming/Desktop/牛邦测试租户1_工厂助手小程序_黎明电脑.txt";
            var fileStream = File.OpenRead(testFilePath);

            _liceseLoaderService.AddLicense(fileStream);

            //await WithUnitOfWorkAsync(CrudExec);



        }

        private async Task CrudExec()
        {

            var warehouseTypeResult = await _warehouseTypeService.CreateAsync(new Nb.FactoryAssistant.Service.Abstracts.Dto.WmsWarehouseTypeCreateUpdateInput
            {
                Code = "UnitTestDataCode",
                Name = "UnitTestName",
                Description = "",
            });

            if (warehouseTypeResult.Success == false)
            {
                throw new Exception(warehouseTypeResult.ErrorMsg);
            }

            var warehouseType = warehouseTypeResult.Data;



            var warehouseTypeUpdateResult = await _warehouseTypeService.UpdateAsync(warehouseType.Id, new Nb.FactoryAssistant.Service.Abstracts.Dto.WmsWarehouseTypeCreateUpdateInput
            {
                 Code= warehouseType.Code+"Update",
                  Description= warehouseType.Description,
                   Name= warehouseType.Name+"Update",
            });

            if (warehouseTypeUpdateResult.Success == false)
            {
                throw new Exception(warehouseTypeUpdateResult.ErrorMsg);
            }
            if (warehouseTypeUpdateResult.Data.Code != warehouseType.Code + "Update")
            {
                throw new Exception("更新失败");
            }

            var warehouseTypeDeleteResult = await _warehouseTypeService.DeleteAsync(new List<Guid> { warehouseType.Id });
            if (warehouseTypeDeleteResult.Success == false)
            {
                throw new Exception("删除失败");
            }
        }
    }
}
